import { OnInit, Component, ViewChild, Input, Output, EventEmitter } from '@angular/core';
import { STComponent, STColumn } from '@delon/abc';
import { ObjectUtil } from 'src/app/project/utils/object-util';
import { TABLE_PARAMETER } from '../../setting/table-parameter';
import { SystemDictionaryValueDTO } from 'src/app/project/model/icm/icm-global/dictionary/dictionary-value.dto';
import { SystemDictionaryDTO } from 'src/app/project/model/icm/icm-global/dictionary/dictionary.dto';
import { DcitionaryValueDetailComponent } from './dictionary-value-detail.component';
import { DictionaryService } from 'src/app/project/service/icm/icm-global/dictionary/dictionary.service';
import { OperationNotification } from 'src/app/project/model/common/operation-notification';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-dictionary-value-list',
  templateUrl: './dictionary-value-list.component.html',
  styles: [
    `
      .ant-modal-body {
        height: 400px;
      }
    `,
  ],
})
export class DictionaryValueListComponent implements OnInit {
  constructor(private dictionaryService: DictionaryService, private msg: NzMessageService) {}

  @ViewChild('dictionaryValueDetailComponent', { static: false })
  dictionaryValueDetailComponent: DcitionaryValueDetailComponent;

  /**
   * 字典值列表所属的字典id
   */
  dictionaryId: string;

  /**
   * 字典值列表所属的字典code
   */
  dictionaryCode: string;
  /**
   * 数据变化通知事件
   */
  @Output()
  notification = new EventEmitter<OperationNotification<SystemDictionaryValueDTO>>();

  /**
   * 表单窗体打开/关闭控制
   */
  isVisible = false;

  /**
   * 遮罩
   */
  loading = false;

  /**
   * 表格组件
   */
  @ViewChild('st', { static: false })
  st: STComponent;

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 表格数据：所有字典值
   */
  tableData: Array<SystemDictionaryValueDTO>;

  /**
   * 表格的列定义
   */
  @Input()
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '5%',
      className: 'text-center',
    },
    {
      title: '键',
      index: 'key',
      width: '10%',
      className: 'text-left',
    },
    {
      title: '值',
      index: 'value',
      width: '10%',
      className: 'text-left',
    },
    {
      title: '是否可用',
      render: 'available',
      width: '10%',
      className: 'text-center',
    },
    {
      title: '备注',
      index: 'remark',
      width: '10%',
      className: 'text-left text-truncate',
    },
    {
      title: '操作',
      render: 'operations',
      index: 'operation',
      width: '10%',
      className: 'text-center',
    },
  ];

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   * @param e event
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      //   this.update(e.dblClick.item);
    }
  }

  /**
   * 修改字典值
   */
  update(row: SystemDictionaryValueDTO) {
    this.dictionaryValueDetailComponent.edit(false, row);
  }

  /**
   * 删除字典值
   */
  onDelete(row: SystemDictionaryValueDTO) {
    this.dictionaryService.deleteValue(this.dictionaryId, row.id).subscribe(() => {
      this.loadData();
      this.msg.success('删除成功');
    });
  }

  /**
   * 加载字典值
   */
  loadData() {
    this.loading = true;
    this.dictionaryService.findValueByCode(this.dictionaryCode, true).subscribe(
      data => {
        this.tableData = data.values;
        this.TABLE_PARAMETER.page.total = data.values.length;
        this.dictionaryId = data.id;
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 打开字典值列表List
   */
  openValueList(code: string) {
    this.dictionaryCode = code;
    this.loadData();
    this.isVisible = true;
  }

  /**
   * 添加字典值
   */
  addValue() {
    this.dictionaryValueDetailComponent.edit(false, null);
  }

  /**
   * 修改窗口状态
   */
  handleCancel() {
    this.isVisible = false;
    this.notification.emit();
  }

  ngOnInit() {}
}
